//
//  MKTChatMsgTVCellTextMine.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/8.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatMsgTextTVCellMine.h"

@implementation MKTChatMsgTextTVCellMine
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //
        self.msgTextBubbleView = [[MKTChatTextBubbleViewMine alloc] init];
        [self.contentView addSubview:self.msgTextBubbleView];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.msgTextBubbleView,self.msgUserPicView] constraintParam:(MKUIFlowLayoutConstraintParam_H_T_R) contentInsets:UIEdgeInsetsMake(5, 55, 5, 20) interitemSpacing:5];
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    [self.mk_tableViewOfFirst mk_dismissSubViewPopoverAnimated:NO completion:nil];//清除已有的pop弹窗
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKTChatMsgText *msg = self.chatMsg;
    NSString *htmlString = [NSString stringWithFormat:@"<span style='display: inline-block;font-size: 14px;color:red;'>%@</span>",msg.text];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,/*NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)*/} documentAttributes:nil error:nil];
    self.msgTextBubbleView.textLabel.attributedText = attr;
    self.msgTextBubbleView.textLabel.textAlignment = NSTextAlignmentLeft;
    self.msgTextBubbleView.textLabel.font = [UIFont systemFontOfSize:14];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (BOOL)enableLongPress{
    return YES;
}
- (void)didLongPress:(CGPoint)point{
    UITableView *tableView = self.mk_tableViewOfFirst;
    if(!tableView)return;
    UIView *contentView = self.msgTextBubbleView;
    CGPoint p2 = [self convertPoint:point toView:contentView];
    if(![contentView pointInside:p2 withEvent:nil]) return;
    
    MKUIPopoverMenuListView *popView = [[MKUIPopoverMenuListView alloc] init];
    [popView addMenu:[MKUIPopoverMenu menuWithTitle:@"复制" whenClick:^(MKUIPopoverMenu * _Nonnull menu) {
        UIPasteboard.generalPasteboard.string = self.msgTextBubbleView.textLabel.text;
    }]];
    [popView addMenu:[MKUIPopoverMenu menuWithTitle:@"复制到上面" whenClick:^(MKUIPopoverMenu * _Nonnull menu) {
        MKTChatMsgText *msg = [[MKTChatMsgText alloc] init];
        msg.text = @"复制到上面";
        msg.sender = self.chatMsg.sender;
        msg.senderType = MKTChatMsgSenderTypeMine;
        MKUITableViewCellModel *cm = [[MKUITableViewCellModel alloc] init];
        cm.cellClass = [MKTChatMsgTextTVCellMine class];
        cm.modelValue = msg;
        [self.cellModel.tableViewModel insertCellModel:cm beforeIndexPath:self.cellModel.indexPathInModel animated:NO];
        //使cm可见
        if(![self.cellModel.tableView.mk_indexPathsForRowsInVisibleContentBounds containsObject:cm.indexPathInModel]){
            [self.cellModel.tableView scrollToRowAtIndexPath:cm.indexPathInModel atScrollPosition:(UITableViewScrollPositionTop) animated:YES];
        }
    }]];
    [popView addMenu:[MKUIPopoverMenu menuWithTitle:@"复制到下面" whenClick:^(MKUIPopoverMenu * _Nonnull menu) {
        MKTChatMsgText *msg = [[MKTChatMsgText alloc] init];
        msg.text = @"复制到下面";
        msg.sender = self.chatMsg.sender;
        msg.senderType = MKTChatMsgSenderTypeMine;
        MKUITableViewCellModel *cm = [[MKUITableViewCellModel alloc] init];
        cm.cellClass = [MKTChatMsgTextTVCellMine class];
        cm.modelValue = msg;
        [self.cellModel.tableViewModel insertCellModel:cm afterIndexPath:self.cellModel.indexPathInModel animated:NO];
        //使cm可见
        if(![self.cellModel.tableView.mk_indexPathsForRowsInVisibleContentBounds containsObject:cm.indexPathInModel]){
            [self.cellModel.tableView scrollToRowAtIndexPath:cm.indexPathInModel atScrollPosition:(UITableViewScrollPositionBottom) animated:YES];
        }
    }]];
    [popView presentPopoverPointToView:contentView inContainerView:tableView animated:YES completion:nil];
}
@end
